﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static cszmcaux.Zmcaux;

namespace ZmotionFrame
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            cb_Axis.SelectedIndex = 0;
            rb_Realtive.Checked = true;
            rb_Forward.Checked = true;
        }
        IntPtr handle;
        private void btn_Connection_Click(object sender, EventArgs e)
        {
            try
            {
                int sre =ZAux_OpenEth(cb_IP.Text.ToString(), out IntPtr handle);
                if (sre == 0)
                {
                    lb_LinkStatus.Text = "已连接";
                    lb_LinkStatus.BackColor = Color.Green;
                    StringBuilder SoftType = new StringBuilder();
                    StringBuilder SoftVersion = new StringBuilder();
                    StringBuilder ControllerID = new StringBuilder();
                    var res = ZAux_GetControllerInfo(handle, SoftType, SoftVersion, ControllerID);
                    int piValue=-1;
                    ZAux_Direct_GetAxisAddress(handle,1,ref piValue);
                }
                else
                {
                    lb_LinkStatus.Text = "连接失败";
                    lb_LinkStatus.BackColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
            
        }

        private void btn_SearchIP_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            int res = ZAux_SearchEthlist(sb,1024,1000);
            if (res != 0)
            {
                MessageBox.Show("扫描失败");
                return;
            }
            var ips=sb.ToString().Split(" ");
            cb_IP.Items.Clear();
            cb_IP.Items.Add("127.0.0.1");
            if (ips != null)
            {
                for (int i = 0; i < ips.Length - 1; i++)
                {
                    cb_IP.Items.Add(ips[i]);
                }
            }
            cb_IP.SelectedIndex = 0;


        }

        private void btn_CardInit_Click(object sender, EventArgs e)
        {
            int axisNum = -1;
            //获取规划的轴号
            if (cb_Axis.SelectedItem.ToString().Contains("X"))
            {
                axisNum = 0;
            }
            else if (cb_Axis.SelectedItem.ToString().Contains("Y"))
            {
                axisNum = 1;
            }
            //设置正限位IO口
            ZAux_Direct_SetFwdIn(handle, axisNum, 0);
            //设置负限位IO口
            ZAux_Direct_SetRevIn(handle, axisNum, 1);
            //设置原点IO口
            ZAux_Direct_SetDatumIn(handle, axisNum, 2);
        }
        int temp = 0;

        private void btn_PointLeft_MouseDown(object sender, MouseEventArgs e)
        {

            SetSinaleParams();
            ZAux_Direct_Single_Vmove(handle,0,1);
        }

        private void btn_PointRight_MouseDown(object sender, MouseEventArgs e)
        {
            SetSinaleParams();
            ZAux_Direct_Single_Vmove(handle, 0, -1);
        }

        private void btn_PointLeft_MouseUp(object sender, MouseEventArgs e)
        {
            if (handle != IntPtr.Zero)
            {
                int res = ZAux_Direct_Single_Cancel(handle,0,2);
            }
        }

        private void btn_PointRight_MouseUp(object sender, MouseEventArgs e)
        {
            if (handle != IntPtr.Zero)
            {
                int res = ZAux_Direct_Single_Cancel(handle, 0, 2);
            }
        }
        public void SetSinaleParams()
        {
            //轴类型
            ZAux_Direct_SetAtype(handle, 0, 1);
            ZAux_Direct_SetLspeed(handle, 0, Convert.ToSingle(tb_StartSpeed.Text));
            ZAux_Direct_SetSpeed(handle, 0, Convert.ToSingle(tb_RunningSpeed.Text));
            ZAux_Direct_SetAccel(handle, 0, Convert.ToSingle(tb_Acceleration.Text));
            ZAux_Direct_SetDecel(handle, 0, Convert.ToSingle(tb_Deceleration.Text));
            ZAux_Direct_SetSramp(handle, 0, Convert.ToSingle(tb_Sramp.Text));
            ZAux_Direct_SetUnits(handle, 0, Convert.ToSingle(tb_PulseValue.Text));

            ZAux_Direct_Single_Vmove(handle, 0, 1);
        }

        private void btn_StartRun_Click(object sender, EventArgs e)
        {
            var str=uiRadioButtonGroup1.Items;
            var mode = rb_Realtive.Checked ? 1 : -1;
            var dir = rb_Rev.Checked ? 1 : -1;
            SetSinaleParams();
            //快速减速
            ZAux_Direct_SetFastDec(handle, 0, 2000);
            var dis = Convert.ToSingle(tb_Dis.Text);
            //运动模式 1 相对 2 绝对
            if (mode == 1)
            {
                ZAux_Direct_Single_Move(handle, 0, dis * dir);
            }
            else
            {
                ZAux_Direct_Single_MoveAbs(handle, 0, dis);
            }
        }

        private void btn_StopRun_Click(object sender, EventArgs e)
        {
            if (handle != IntPtr.Zero)
            {
                ZAux_Direct_Single_Cancel(handle, 0, 2);
            }
        }

        private void btn_CallBack_Click(object sender, EventArgs e)
        {
           
            if (handle != IntPtr.Zero)
            {
                ZAux_Direct_SetDpos(handle, 0, 0);
            }
        }
    }
}
